package cn.iocoder.yudao.module.wine.controller.app.supplymarket;

import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import cn.iocoder.yudao.module.wine.controller.admin.countnameconfig.vo.CountNameConfigListReqVO;
import cn.iocoder.yudao.module.wine.controller.admin.countnameconfig.vo.CountNameConfigRespVO;
import cn.iocoder.yudao.module.wine.controller.admin.supplyinfo.vo.SupplyInfoRespVO;
import cn.iocoder.yudao.module.wine.service.countnameconfig.CountNameConfigService;
import cn.iocoder.yudao.module.wine.service.supplyinfo.SupplyInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.module.wine.controller.app.supplymarket.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplymarket.SupplyMarketDO;
import cn.iocoder.yudao.module.wine.service.supplymarket.SupplyMarketService;

@Tag(name = "用户 APP - 酒类行情管理")
@RestController
@RequestMapping("/wine/supply-market")
@Validated
public class AppSupplyMarketController {

    @Resource
    private SupplyMarketService supplyMarketService;


    @Resource
    private SupplyInfoService supplyInfoService;

    @Resource
    private CountNameConfigService countNameConfigService;

//    @Resource
//    private DictDataService dictDataService;

    @Resource
    private DictDataApi dataApi;


    @GetMapping("/config-list")
    @Operation(summary = "获取香型配置的酒类信息")
    public CommonResult<List<CountNameConfigRespVO>> getCountNameConfigList(@Valid CountNameConfigListReqVO reqVO) {
        List<CountNameConfigRespVO> list = countNameConfigService.getCountNameConfigList(reqVO);
        return success(list);
    }

    @GetMapping("/list")
    @Operation(summary = "获得酒类行情列表")
    public CommonResult<List<AppSupplyMarketChartRespVO>> getSupplyMarketList(@Valid AppSupplyMarketPageReqVO reqVO) {
        List<SupplyMarketDO> list = supplyMarketService.appGetSupplyMarketList(reqVO);

        List<AppSupplyMarketChartRespVO> respVOList = BeanUtils.toBean(list, AppSupplyMarketChartRespVO.class);

        //获取年份字典、包装字典
//        List<DictDataDO> dictYearList = dictDataService.getDictDataListByDictType("year");
//        List<DictDataDO> dictPackingList = dictDataService.getDictDataListByDictType("packing");
//        respVOList.forEach(item -> {
//            dictYearList.stream().filter(yItem -> item.getShowYear().toString().equals(yItem.getValue())).findFirst().ifPresent(yearDO -> item.setShowYearName(yearDO.getLabel()));
//            dictPackingList.stream().filter(yItem -> item.getPacking().toString().equals(yItem.getValue())).findFirst().ifPresent(packDO -> item.setPackingName(packDO.getLabel()));
//        });

        List<DictDataRespDTO> packing = dataApi.getDictDataList("packing");
        List<DictDataRespDTO> year = dataApi.getDictDataList("year");

        List<AppSupplyMarketChartRespVO> collect = new ArrayList<>();

        for (AppSupplyMarketChartRespVO item : respVOList) {
            for (DictDataRespDTO dictDataRespDTO : packing) {
                if (dictDataRespDTO.getValue().equals(String.valueOf(item.getPacking()))) {
                    item.setPackingName(dictDataRespDTO.getLabel());
                    break;
                }
            }
            for (DictDataRespDTO dictDataRespDTO : year) {
                if (dictDataRespDTO.getValue().equals(String.valueOf(item.getShowYear()))) {
                    item.setShowYearName(dictDataRespDTO.getLabel());
                    break;
                }
            }
            collect.add(item);
        }

        return success(collect);
    }

    @GetMapping("/info-list")
    @Operation(summary = "获得酒类行情下的供求信息列表")
    public CommonResult<PageResult<SupplyInfoRespVO>> getSupplyMarketList( @Valid MarketInfoListPageReqVO reqVo) {
        PageResult<SupplyInfoRespVO> page = supplyInfoService.appGetSupplyMarketInfoList(reqVo);
        return success(page);
    }

    @GetMapping("/info-page")
    @Operation(summary = "获得酒类行情下的供求信息列表分页")
    public CommonResult<PageResult<SupplyInfoRespVO>> getSupplyMarketPage(@Valid InfoPageReqVO reqVO) {
        PageResult<SupplyInfoRespVO> page = supplyInfoService.appGetSupplyMarketInfoPage(reqVO);
        return success(page);
    }

    @GetMapping("/home-page-list")
    @Operation(summary = "首页获得酒类行情列表")
    public CommonResult<List<AppSupplyMarketRespVO>> getSupplyMarketListByConfig(@Valid AppSupplyMarketPageReqVO reqVO) {
        List<SupplyMarketDO> list = supplyMarketService.getSupplyMarketListByConfig(reqVO);
        return success(BeanUtils.toBean(list, AppSupplyMarketRespVO.class));
    }

    @GetMapping("/config-page")
    @Operation(summary = "首页获得酒类行情配置列表")
    public CommonResult<PageResult<CountNameConfigRespVO>> getMarketConfigList(@Valid AppSupplyMarketPageReqVO reqVO) {
        PageResult<CountNameConfigRespVO> list = supplyMarketService.getMarketConfigList(reqVO);
        return success(BeanUtils.toBean(list, CountNameConfigRespVO.class));
    }

}
